//
//  CODetailHomeTableViewCell.swift
//  CocoProject
//
//  Created by Ruthless on 2023/8/22.
//

import UIKit
import QMUIKit

class CODetailHomeTableViewCell: UITableViewCell {
    var clickAction:(()->Void)?
    var likeAction: (()->Void)?
    var headerAction:(()->Void)?
    var flowAction:(()->Void)?
    var model: COQADetailHeaderModel? {
        didSet {
            guard let model = model else {
                return
            }
            
            avatar.kf.setImage(with: URL(string: model.avatar))
            nameLabel.text = model.name
            levelImageView.isHidden = !model.showID
            fallowButton.isSelected = model.isFollow
            timeLabel.text = model.time
            tagLabel.setTitle(model.tag, for: .normal)
            contentLabel.text = model.content
            
            likeButton.isHidden = !model.showButton
            flowButton.isHidden = !model.showButton
            
            likeButton.isSelected = model.like
            likeButton.setTitle("\(model.likeNum)", for: .normal)
            flowButton.setTitle("\(model.flowNum)", for: .normal)
            
            if let id = COUserManager.userInfo?.id {
                fallowButton.isHidden = id == model.id
            }
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        configUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func configUI() {
        backgroundColor = .rgba(17, 7, 41, 1)
        contentView.addSubview(avatar)
        contentView.addSubview(nameLabel)
        contentView.addSubview(levelImageView)
        contentView.addSubview(timeLabel)
        contentView.addSubview(fallowButton)
        contentView.addSubview(tagLabel)
        contentView.addSubview(contentLabel)
        contentView.addSubview(lineView)
        contentView.addSubview(likeButton)
        contentView.addSubview(flowButton)
        
        avatar.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(20)
            make.top.equalToSuperview().offset(18)
            make.width.height.equalTo(30)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(avatar.snp.right).offset(5)
            make.top.equalTo(avatar.snp.top).offset(0)
        }
        
        levelImageView.snp.makeConstraints { make in
            make.left.equalTo(nameLabel.snp.right).offset(5)
            make.centerY.equalTo(nameLabel)
        }
        
        timeLabel.snp.makeConstraints { make in
            make.left.equalTo(avatar.snp.right).offset(5)
            make.bottom.equalTo(avatar.snp.bottom).offset(0)
        }
        
        fallowButton.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-20)
            make.top.equalToSuperview().offset(15)
        }
        
        tagLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(20)
            make.top.equalTo(avatar.snp.bottom).offset(20)
        }
        
        contentLabel.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(20)
            make.top.equalTo(tagLabel.snp.bottom).offset(4)
        }
        
        lineView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(contentLabel.snp.bottom).offset(55)
            make.height.equalTo(10)
        }
        
        likeButton.snp.makeConstraints { make in
            make.right.equalTo(flowButton.snp.left).offset(-25)
            make.centerY.equalTo(flowButton)
        }
        
        flowButton.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-20)
            make.bottom.equalToSuperview().offset(-20)
        }
    }

    private lazy var avatar: UIImageView = {
        let imgV = UIImageView()
        imgV.layer.masksToBounds = true
        imgV.layer.cornerRadius = 15
        imgV.backgroundColor = .rgba(17, 7, 41, 1)
        
        let tap = UITapGestureRecognizer(target: self, action: #selector(clickAvatar))
        imgV.addGestureRecognizer(tap)
        imgV.isUserInteractionEnabled = true
        return imgV
    }()
    
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = .medium(15)
        label.textColor = .white
        return label
    }()
    
    private lazy var levelImageView: UIImageView = {
        let imgV = UIImageView(image: UIImage(named: "Advanced"))
        return imgV
    }()
    
    private lazy var timeLabel: UILabel = {
        let label = UILabel()
        label.font = .medium(12)
        label.textColor = .white.withAlphaComponent(0.5)
        return label
    }()
    
    private(set) lazy var fallowButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "编组 12"), for: .selected)
        btn.setImage(UIImage(named: "编组 22"), for: .normal)
        btn.addTarget(self, action: #selector(clickButton), for: .touchUpInside)
        return btn
    }()
    
    private lazy var tagLabel: QMUIButton = {
        let btn = QMUIButton()
        btn.titleLabel?.font = .regular(13)
        btn.contentEdgeInsets = .init(top: 3, left: 7, bottom: 3, right: 7)
        btn.setTitleColor(.white, for: .normal)
        btn.layer.masksToBounds = true
        btn.layer.cornerRadius = 10
        btn.layer.borderColor = UIColor.white.cgColor
        btn.layer.borderWidth = 1
        return btn
    }()
    
    private lazy var contentLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(15)
        label.textColor = .white
        label.numberOfLines = 0
        return label
    }()
    
    private lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = .white.withAlphaComponent(0.15)
        return view
    }()
    
    private lazy var likeButton: QMUIButton = {
        let btn = QMUIButton()
        btn.spacingBetweenImageAndTitle = 4
        btn.setTitle("0", for: .normal)
        btn.setImage(UIImage(named: "编组 20"), for: .selected)
        btn.setImage(UIImage(named: "编组 11"), for: .normal)
        btn.titleLabel?.font = .regular(12)
        btn.setTitleColor(.white, for: .normal)
        btn.addTarget(self, action: #selector(clickButton), for: .touchUpInside)
        return btn
    }()
    
    private lazy var flowButton: QMUIButton = {
        let btn = QMUIButton()
        btn.spacingBetweenImageAndTitle = 4
        btn.setTitle("0", for: .normal)
        btn.setImage(UIImage(named: "位图备份 2"), for: .normal)
        btn.titleLabel?.font = .regular(12)
        btn.setTitleColor(.white, for: .normal)
        btn.addTarget(self, action: #selector(clickButton), for: .touchUpInside)

        return btn
    }()
}

extension CODetailHomeTableViewCell {
    @objc
    private func clickButton(_ btn: UIButton) {
        if btn == fallowButton {
            clickAction?()
        } else if btn == likeButton {
            likeAction?()
        } else if btn == flowButton {
            flowAction?()
        }
        
    }
    
    @objc
    private func clickAvatar() {
        headerAction?()
    }
}
